package com.lyb.hrm.service.impl;

import com.lyb.hrm.feignclients.RedisFeignClient;
import com.lyb.hrm.service.IVerifyCodeService;
import com.lyb.hrm.util.VerifyCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.UUID;

@Service
public class VerifyCodeServiceImpl implements IVerifyCodeService {

    @Autowired
    private RedisFeignClient redisFeignClient ;

    /**
     //1.判断参数
     //2.使用UUID生成验证码的值
     //3.以imageCodeKey为key把验证码的值存储到Redis
     //4.把验证码的值合并成图片
     //5.把图片进行base64编码
     //6.返回编码后的字符串
     */
    @Override
    public String createImageCode(String imageCodeKey) throws IOException {
        // 判断参数
        if(!StringUtils.hasLength(imageCodeKey)){
            throw new RuntimeException("无效的imageCodeKey");
        }
        // 使用UUID生成验证码的值
        String imageCodeValue = UUID.randomUUID().toString().substring(0,4);
        // 以imageCodeKey为key把验证码的值存储到Redis , 设置过期时间
        redisFeignClient.setex(imageCodeKey,300,imageCodeValue);
        // 在工具类中完成后面几步
        return VerifyCodeUtils.verifyCode(135,40,imageCodeValue);
    }
}
